package cn.akwangl.concurrency.web.model.order.service.impl;

import cn.akwangl.concurrency.web.exception.BizException;
import cn.akwangl.concurrency.web.model.items.pojo.ItemsEntity;
import cn.akwangl.concurrency.web.model.items.pojo.ItemsImgEntity;
import cn.akwangl.concurrency.web.model.items.pojo.ItemsSpecEntity;
import cn.akwangl.concurrency.web.model.items.service.ItemsImgService;
import cn.akwangl.concurrency.web.model.items.service.ItemsService;
import cn.akwangl.concurrency.web.model.items.service.ItemsSpecService;
import cn.akwangl.concurrency.web.model.order.pojo.*;
import cn.akwangl.concurrency.web.model.order.service.CreateOrdersService;
import cn.akwangl.concurrency.web.model.order.service.OrderItemsService;
import cn.akwangl.concurrency.web.model.order.service.OrderStatusService;
import cn.akwangl.concurrency.web.model.order.service.OrdersService;
import cn.akwangl.concurrency.web.model.user.pojo.UserAddressEntity;
import cn.akwangl.concurrency.web.model.user.service.UserAddressService;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表; 服务实现类
 * </p>
 *
 * @author 阿坤同学
 * @since 2022-11-22 14:29:53
 */
@Service("ordersVersion")
public class OrdersVersionServiceImpl implements CreateOrdersService {

    private UserAddressService userAddressService;

    private ItemsSpecService itemsSpecService;

    private ItemsService itemsService;

    private ItemsImgService itemsImgService;

    private OrderItemsService orderItemsService;

    private OrderStatusService orderStatusService;

    private OrdersService ordersService;

    @Autowired
    public void setUserAddressService(UserAddressService userAddressService) {
        this.userAddressService = userAddressService;
    }

    @Autowired
    public void setItemsSpecService(ItemsSpecService itemsSpecService) {
        this.itemsSpecService = itemsSpecService;
    }

    @Autowired
    public void setItemsService(ItemsService itemsService) {
        this.itemsService = itemsService;
    }

    @Autowired
    public void setItemsImgService(ItemsImgService itemsImgService) {
        this.itemsImgService = itemsImgService;
    }

    @Autowired
    public void setOrderItemsService(OrderItemsService orderItemsService) {
        this.orderItemsService = orderItemsService;
    }

    @Autowired
    public void setOrderStatusService(OrderStatusService orderStatusService) {
        this.orderStatusService = orderStatusService;
    }

    @Autowired
    public void setOrdersService(OrdersService ordersService) {
        this.ordersService = ordersService;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrderVo createOrder(SubmitOrderDto submitOrderDto) {
        String addressId = submitOrderDto.getAddressId();
        List<ShopCart> shopCarts = submitOrderDto.getShopCarts();

        List<String> itemIds = new ArrayList(shopCarts.size());
        List<String> itemSpecIds = new ArrayList(shopCarts.size());
        for (ShopCart itemSpec : shopCarts) {
            String itemId = itemSpec.getItemId();
            String itemSpecId = itemSpec.getItemSpecId();

            itemIds.add(itemId);
            itemSpecIds.add(itemSpecId);
        }

        // 获取商品信息
        Map<String, ItemsEntity> itemsEntityMap = itemsService.getItemsEntityMap(itemIds);
        Map<String, ItemsImgEntity> itemsImgEntityMap = itemsImgService.getItemsImgEntityIsMainMap(itemIds);
        Map<String, ItemsSpecEntity> itemsSpecEntityMap = itemsSpecService.getItemsSpecEntityMap(itemSpecIds);

        // 订单Id
        String orderId = IdWorker.getIdStr();
        // 邮费
        int postAmount = 0;
        // 账单总价
        Integer totalAmount = 0;
        // 实际付款总价
        Integer realPayAmount = 0;
        List<OrderItemsEntity> orderItemsEntities = new ArrayList<>(shopCarts.size());
        for (ShopCart shopCart : shopCarts) {
            String itemsEntityMapKey = shopCart.getItemId();
            String itemsSpecEntityMapKey = shopCart.getItemSpecId();
            Integer buyCounts = shopCart.getBuyCounts();
            ItemsEntity itemsEntity = itemsEntityMap.get(itemsEntityMapKey);
            ItemsSpecEntity itemsSpecEntity = itemsSpecEntityMap.get(itemsSpecEntityMapKey);
            ItemsImgEntity itemsImgEntity = itemsImgEntityMap.get(itemsEntityMapKey);

            OrderItemsEntity orderItems = OrderFactory.createOrderItemsEntity(buyCounts, orderId, itemsEntity, itemsSpecEntity, itemsImgEntity);
            orderItemsEntities.add(orderItems);
            // 统计订单总价
            totalAmount += itemsSpecEntity.getPriceNormal() * buyCounts;
            realPayAmount += itemsSpecEntity.getPriceDiscount() * buyCounts;
        }
        // 保存订单明细
        orderItemsService.saveBatch(orderItemsEntities);
        // 扣减库存
        boolean decreaseStockFlag = itemsSpecService.decreaseItemSpecStockVersion(shopCarts);
        if (!decreaseStockFlag) {
            throw new BizException("库存扣减失败");
        }
        // 获取用户地址
        UserAddressEntity userAddressEntity = userAddressService.getById(addressId);
        // 创建订单
        OrdersEntity ordersEntity = OrderFactory.createOrderEntity(orderId, postAmount, totalAmount, realPayAmount, submitOrderDto, userAddressEntity);
        ordersService.save(ordersEntity);
        // 创建订单状态
        OrderStatusEntity orderStatusEntity = OrderFactory.createOrderStatusEntity(ordersEntity);
        orderStatusService.save(orderStatusEntity);
        // 创建商户信息
        MerchantOrdersVo merchantOrdersVo = OrderFactory.createMerchantOrdersVo(ordersEntity);
        // 创建订单VO
        return OrderFactory.createOrderVo(orderId, shopCarts, merchantOrdersVo);
    }
}
